﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.Bliss.Services;
using com.Bliss;
using com.Bliss.ServiceFacade.Post.Request;
using com.Bliss.ServiceFacade.User.Request;
using System.Data;
using System.Configuration;

namespace mojoPortal.Web.RS.Bliss.Controls
{
    public partial class UserGroups : com.Bliss.UI.Common.UserControlBase
    {
        com.Bliss.UI.Common.DataBaseClass dbClass = new com.Bliss.UI.Common.DataBaseClass();
        public DataTable dt;
        public string OP_SSOToken
        {
            get
            {
                return Convert.ToString(ViewState["OP_SSOToken"]);
            }
            set
            {
                ViewState["OP_SSOToken"] = value;
            }
        }
        public int OP_LoggedInUserId
        {
            get
            {
                int returnValue = 0;
                int.TryParse(Convert.ToString(ViewState["OP_LoggedInUserId"]), out returnValue);
                return returnValue;
            }
            set
            {
                ViewState["OP_LoggedInUserId"] = value.ToString();
            }
        }
        public int OP_ProfileUserId
        {
            get
            {
                int returnValue = 0;
                int.TryParse(Convert.ToString(ViewState["OP_ProfileUserId"]), out returnValue);
                return returnValue;
            }
            set
            {
                ViewState["OP_ProfileUserId"] = value.ToString();
            }
        }
        private int OP_CompanyID
        {
            get
            {
                int returnValue = 0;
                int.TryParse(Convert.ToString(ViewState["OP_CompanyID"]), out returnValue);
                return returnValue;
            }
            set
            {
                ViewState["OP_CompanyID"] = value.ToString();
            }
        }
        private string OP_UserName
        {
            get
            {
                return Convert.ToString(ViewState["OP_UserName"]);
            }
            set
            {
                ViewState["OP_UserName"] = value;
            }
        }
        private string OP_ImageName
        {
            get
            {
                return Convert.ToString(ViewState["OP_ImageName"]);
            }
            set
            {
                ViewState["OP_ImageName"] = value;
            }
        }
        public List<IGroup> lstGroups = new List<IGroup>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (OP_SSOToken == string.Empty)
                {
                    ExceptionMessage = "User Token propertiey not set.";
                    return;
                }
                //else if (OP_ProfileUserId == 0)
                //{
                //    ExceptionMessage = "ProfileUserId propertiey not set.";
                //    return;
                //}
                else
                {
                    IUserService service = new UserService();
                    var user = service.ValidateUserSsoToken(new ValidateUserSsoTokenRequest() { SsoToken = OP_SSOToken });
                    if (user.Data == null)
                    {
                        ExceptionMessage = "Invalid User Token provided";
                        return;
                    }
                    else
                    {
                        OP_CompanyID = user.Data.CompanyID;
                        OP_LoggedInUserId = user.Data.ID;
                        OP_UserName = user.Data.Email;
                        OP_ImageName = user.Data.ImageName;
                        OP_ProfileUserId = user.Data.ID;
                    }
                }
                GetUserGroups(OP_ProfileUserId);
            }
        }

        public void GetUserGroups(int userId)
        {
            IUserService service = new UserService();
            var res = service.GetUserGroups(new GetUserGroupsRequest() { UserId = userId });
            lstGroups = res.DataCollection.ToList();
            GroupList.DataSource = lstGroups;
            GroupList.DataBind();
        }

        public string getHREF(object sURL)
        {
            var dRView = (IGroup)sURL;
            string Id = dRView.Id.ToString();
            return ResolveUrl("~/GroupDetail.aspx?Id=" + Id);
        }
        public new string getSRC(object imgSRC)
        {
            var dRView = (IGroup)imgSRC;
            string ImageName = dRView.ImageName;
            if (ImageName == "NoImage")
            {
                return ConfigurationManager.AppSettings["ImageUrl"].ToString() + @"/image_missing.jpg";
            }
            else
            {
                return ConfigurationManager.AppSettings["ImageUrl"].ToString() + @"/" + ImageName;
            }
        }
        public string getName(object nameSRC)
        {
            var dRView = (IGroup)nameSRC;
            return dRView.GroupName;
        }
    }
}